package bairui.international.weixin.portal.miniapp.controller.login.login;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.lianjing.sh.validate.spring.exception.ValidateObjectException;

import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.type.Type_User;
import bairui.international.service.Service_User;
import bairui.international.weixin.portal.miniapp.controller.login.C_Login_Login.Output_Login;
import bairui.international.weixin.portal.miniapp.controller.login.C_Login_Login.Output_User;
import lombok.Getter;
import lombok.Setter;

/**
 *
 * 登录
 *
 * @generated
 * 
 * @see STest_C_Login_Login_Login_Post 测试
 *
 */
@Controller
public class C_Login_Login_Login_Post
 extends SC_Login_Login_Login_Post 
{

	@Autowired private Service_User service_User;

	/**
	 * Post请求
	 */
	@Action(Post.class)
	@PostMapping(Post.RequestMappingValue)
	public @FomatOutput @ResponseBody Object post(
		ModelMap map, HttpServletRequest request,
		@RequestParam String skey,
		@RequestBody Input input
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		String name = input.getName();
		String password = input.getPassword();
		// 验证
		if(null == name || name.trim().equals("")){	
			throw new ValidateObjectException("object", "", "用户名不能为空");
		}
		if(null == password || password.trim().equals("")){
			throw new ValidateObjectException("object", "", "密码不能为空");
		}
		// 登录
		if(name.equals("cheshi_abc1") && password.equals("abc123")){
			user = service_User.setType(skey, Type_User.Agent);
		}else if(name.equals("cheshi_abc2") && password.equals("abc123")){
			user = service_User.setType(skey, Type_User.Partner);
		}else if(name.equals("cheshi_abc3") && password.equals("abc123")){
			user = service_User.setType(skey, Type_User.StrategyPartner);
		}else{
			throw new ValidateObjectException("object", "", "用户名或密码错误");
		}
		//
		return new Output_Login(user.getSkey(), Output_Login.State.OK, null, new Output_User(user.getId(), user.getNickName(), user.getType()));
	}
	
	public static class Input {
		@Getter @Setter private String password;
		@Getter @Setter private String name;
	}
	
}
